#include "MenuState.h"

#include "..\..\SGD Wrappers\SGD_AudioManager.h"
#include "..\..\SGD Wrappers\SGD_GraphicsManager.h"
#include "..\..\SGD Wrappers\SGD_InputManager.h"

#include "..\Game.h"
#include "..\Bitmap Font\BitmapFont.h"
#include "ProfileState.h"
#include "OptionsState.h"
#include "CreditsState.h"
#include "GameTimeAttack.h"
#include "HighscoreState.h"

/**************************************************************/
// GetInstance
//	- store the ONLY instance in global memory
//	- return the only instance by pointer
/*static*/ MenuState* MenuState::GetInstance(void)
{
	static MenuState s_Instance;

	return &s_Instance;
}

void MenuState::Enter()
{
	// Get Background Image
	hBackground = GRAPHICS->LoadTexture(L"Resource/Images/MenuBackground.png");

	GAME->SetBGM("Resource/Audio/Beautiful_Chinese_Music_-_Cherry_Blossoms.xwm");

	GAME->PlayBGM();
}

void MenuState::Exit()
{
	// Unload assets
	GRAPHICS->UnloadTexture(hBackground);
}

bool MenuState::Update(float dt)
{
	SGD::InputManager* Input = SGD::InputManager::GetInstance();

	// Press Escape to quit
	if (KEYESCAPE)
		nCursor = 5;

	// Move cursor
	if (KEYDOWNPRESSED)
	{
		GAME->SetInput(false);
		++nCursor;

		// Wrap around
		if (nCursor > 5)
			nCursor = 0;
	}
	else if (KEYUPPRESSED)
	{
		GAME->SetInput(false);
		--nCursor;

		if (nCursor < 0)
			nCursor = 5;
	}

	// Select option
	if (KEYENTER)
	{
		// Which option
		if (nCursor == 0)
		{
			// Change State
			GAME->ChangeState(ProfileState::GetInstance());
			
			// Exit State
			return true;
		}
		else if (nCursor == 1)
		{
			// Change State
			GAME->ResetTimeAttackTime();
			GAME->SetTimeAttackLevel(1);
			GAME->ChangeState(GameTimeAttack::GetInstance());
		}
		else if (nCursor == 2)
		{
			// Change State
			GAME->ChangeState(OptionsState::GetInstance());
			
			// Exit State
			return true;
		}
		else if (nCursor == 3)
		{
			GAME->ChangeState(HighscoreState::GetInstance());

			return true;
		}
		else if (nCursor == 4)
		{
			// Change State
			GAME->ChangeState(CreditsState::GetInstance());

			// Exit State
			return true;
		}
		else if (nCursor == 5)
		{
			return false;
		}
	}
	return true;
}

void MenuState::Render()
{
	// Display Background
	GRAPHICS->DrawTexture(hBackground, SGD::Point{ 0, 0 });

	// Use game font

	// Align text based on window width
	float width = GAME->GetScreenWidth();

	// Display the game title
	//Graphics->DrawString("Fantasy Fall", SGD::Point{ 100, 100 }, SGD::Color{ 255, 50, 50 });
	GRAPHICS->DrawTexture(GAME->hFantasyFallLogoImage, SGD::Point{ 125, 110 });

	// Display menu options
	SGD::Color notSelected = { 255, 225, 100 };
	SGD::Color Play, Time, Options, Highscores, Credits, Exit;
	Play = Time = Options = Highscores = Credits = Exit = notSelected;

	switch (nCursor)
	{
	case 0:
		Play = SGD::Color{ 255, 0, 0 };
		break;
	case 1:
		Time = SGD::Color{ 255, 0, 0 };
		break;
	case 2:
		Options = SGD::Color{ 255, 0, 0 };
		break;
	case 3:
		Highscores = SGD::Color{ 255, 0, 0 };
		break;
	case 4:
		Credits = SGD::Color{ 255, 0, 0 };
		break;
	case 5:
		Exit = SGD::Color{ 255, 0, 0 };
		break;
	}
	GAME->GetBitmapFont()->Render("Start Game", SGD::Point{ 300, 285 }, 0.5f, Play);
	GAME->GetBitmapFont()->Render("Time Attack", SGD::Point{ 300, 320 }, 0.5f, Time);
	GAME->GetBitmapFont()->Render("Options", SGD::Point{ 300, 355 }, 0.5f, Options);
	GAME->GetBitmapFont()->Render("Highscores", SGD::Point{ 300, 390 }, 0.5f, Highscores);
	GAME->GetBitmapFont()->Render("Credits", SGD::Point{ 300, 425 }, 0.5f, Credits);
	GAME->GetBitmapFont()->Render("Exit", SGD::Point{ 300, 460 }, 0.5f, Exit);
	
	GAME->GetBitmapFont()->Render("Up/Down To Navigate, Enter/Button A To Select", SGD::Point{ 75, 525 }, 0.5f, SGD::Color{ 0, 250, 250 });
	GAME->GetBitmapFont()->Render("Escape/Button B To Go To Exit", SGD::Point{ 75, 550 }, 0.5f, SGD::Color{ 0, 250, 250 });
}
